// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html

var Take6Model = require("Take6Model");

cc.Class({
    extends: cc.Component,

    properties: {
        bCanBeTouched : false,
        nCardNum : 0,
        nPunchCount : 0,
        nPlayerIndex : -1,
        bSelected : false,
        fist: cc.SpriteFrame,
        point2BaseColor: cc.SpriteFrame,
        point3BaseColor: cc.SpriteFrame,  
        point5BaseColor: cc.SpriteFrame,  
        point7BaseColor: cc.SpriteFrame,  

        scoreMark: {
            default: null,
            type: cc.Prefab
        },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad : function ()
    {
        // Array test
        // var cards = [1, 2, 3, 4, 5];
        // cc.log("card 5's index is ", cards.indexOf(5));
        // cc.log("current cards ", cards);
        // cards.splice(1, 1);
        // cc.log("current cards after splice ", cards);

        // this.node.on(cc.Node.EventType.TOUCH_START, function (event) 
        // {
        //     cc.log('Mouse down');
            
        // }, this);

        this.node.on('touchmove', function ( event ) 
        {
            if (this.bCanBeTouched == true && Take6Model.m_bInSendCardPhase)
            {
                var pos = cc.find("Canvas").convertToNodeSpaceAR(event.getLocation());
                this.node.setPosition(pos);
                this.node.setLocalZOrder(20);

                cc.find("Canvas").emit('event_player_moving_cards', {});
            }
    
        }, this);

        this.node.on('touchend', function ( event )
        {
            if (this.bCanBeTouched == false || Take6Model.m_bInSendCardPhase == false)
            {
                return;
            }

            var pos = cc.find("Canvas").convertToNodeSpace(event.getLocation());
            if (pos.y > Take6Model.m_flMaskDownHeight && 
                pos.y + Take6Model.m_flMaskUpHeight < cc.view.getVisibleSize().height)
            {
                this.showCardMask();
                this.bSelected = true;

                cc.find("Canvas").emit('event_end_player_progress');
                Take6Model.playerSendCardToServer(this.nCardNum);
            }
            else
            {
                cc.find("Canvas").emit('event_player_cards_reorder', {});
            }   
        }, this);
    },

    setCardNum: function(nCardNum)
    {
        var node = this.node;
        this.nCardNum = nCardNum;

        cc.log("nCardNum : ", nCardNum);
        cc.log("this.nCardNum : ", this.nCardNum);

        var cardNum = node.getChildByName("cardNum");
        cardNum.getComponent(cc.Label).string = this.nCardNum.toString();

        var cardNumSmall = node.getChildByName("cardNumSmall");
        cardNumSmall.getComponent(cc.Label).string = this.nCardNum.toString();

        var cardBaseColor = node.getChildByName("cardBaseColor");
    
        var nNum = parseInt(this.nCardNum);
        if (nNum == 55)
        {
            this.nPunchCount = 7;
            cardBaseColor.getComponent(cc.Sprite).spriteFrame = this.point7BaseColor;
        }
        else if (nNum > 10 && (parseInt(nNum / 10) == parseInt(nNum % 10)))
        {
            this.nPunchCount = 5;
            cardBaseColor.getComponent(cc.Sprite).spriteFrame = this.point5BaseColor;
        }
        else if (nNum % 10 == 0)
        {
            this.nPunchCount = 3;
            cardBaseColor.getComponent(cc.Sprite).spriteFrame = this.point3BaseColor;
        }
        else if (nNum % 10 == 5)
        {
            this.nPunchCount = 2;
            cardBaseColor.getComponent(cc.Sprite).spriteFrame = this.point2BaseColor;
        }
        else
        {
            this.nPunchCount = 1;
        }

        for (var i = 0; i < this.nPunchCount; i++)
        {
            var scoreMark = cc.instantiate(this.scoreMark);
            scoreMark.setPosition(cc.p(50 - i * 20, 90));
            if (i > 4)
            {
                scoreMark.setPosition(cc.p(55 - (i - 5) * 20, 70));
            }
            this.node.addChild(scoreMark);
        }

        var cardScore = node.getChildByName("cardScore");
        cardScore.getComponent(cc.Label).string = this.nPunchCount.toString();
        cardScore.active = false;
    },

    setWithMask : function()
    {
        this.bCanBeTouched = false;

        var cardMask = this.node.getChildByName("cardMask");
        cardMask.setLocalZOrder(1);
        var cardBack = cardMask.getChildByName("cardBack");
        cardBack.setPosition(cc.p(0, 0));
    },

    showCardMask : function()
    {
        this.bCanBeTouched = false;

        var cardMask = this.node.getChildByName("cardMask");
        cardMask.setLocalZOrder(1);
        var cardBack = cardMask.getChildByName("cardBack");
        cardBack.runAction(cc.moveBy(1.0, cc.p(0, -214)));
    },

    hideCardMask : function(flDelay)
    {
        var flDt = 0.7;

        var cardMask = this.node.getChildByName("cardMask");
        cardMask.setLocalZOrder(1);
        var cardBack = cardMask.getChildByName("cardBack");
        cardBack.runAction(cc.sequence(cc.delayTime(flDelay), cc.moveBy(flDt, cc.p(0, 214))));

        var nameBoardTopNode = this.node.getChildByName("nameBoardTop");
        nameBoardTopNode.active = true;
        nameBoardTopNode.opacity = 0;
        nameBoardTopNode.runAction(cc.sequence(cc.delayTime(flDelay + 0.2), cc.fadeIn(flDt - 0.2)));

        return flDt + flDelay;
    },

    hideTopNameBoard : function()
    {
        var nameBoardTopNode = this.node.getChildByName("nameBoardTop");
        nameBoardTopNode.runAction(cc.sequence(cc.fadeOut(0.2), cc.Hide()));
    },

    setPlayerIndex : function(nPlayerIndex)
    {
        this.nPlayerIndex = nPlayerIndex;

        var strPlayerName = Take6Model.m_rgPlayersInfo[nPlayerIndex].strPlayerName;

        var cardMaskNode = this.node.getChildByName('cardMask');
        var nameOnBackLabel = cardMaskNode.getChildByName('cardBack').getChildByName('nameOnBack').getComponent(cc.Label);
        nameOnBackLabel.string = strPlayerName;

        var nameOnTopLabel = this.node.getChildByName('nameBoardTop').getChildByName('nameOnTop').getComponent(cc.Label);
        nameOnTopLabel.string = strPlayerName;
    },

    onPress : function()
    {
        // var node = this.node;
        // var children = this.node.children;
        // cc.log( parseInt(children.length) );

        // cc.log(node.name);
        // cc.log(children[0].name);
        // cc.log(children[1].name);

        // var cardNum = node.getChildByName("cardNum");
        // cardNum.getComponent(cc.Label).string = '102';

        // MODEL test
        // var config = require("TokeModel");
        // config.addSpeed();
        // cc.log("speed is", config.moveSpeed);
    },

    start () {

    },

    // update (dt) {},
});
